package com.lxw.rmg.menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxw.common.utils.PageUtils;
import com.lxw.common.utils.R;
import com.lxw.rmg.menu.entity.MatterEntity;
import com.lxw.rmg.menu.entity.MenuEntity;
import com.lxw.rmg.menu.entity.SubscribeEntity;
import com.lxw.rmg.menu.service.MatterService;
import com.lxw.rmg.menu.service.MenuService;
import com.lxw.rmg.menu.service.SubscribeService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 订餐管理
 *
 * @author lxw
 * @email
 * @date 2023-03-21 22:04:39
 */
@RestController
@RequestMapping("menu/order")
public class OrderController {
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MatterService matterService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("menu:subscribe:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subscribeService.queryPage(params);

        List<SubscribeEntity> list = subscribeService.list();
        page.setList(list);


        //获取当前时间
        String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
        List<SubscribeEntity> subscribeEntityList = (List<SubscribeEntity>) page.getList();

        ArrayList<SubscribeEntity> subscribeEntities = new ArrayList<>();
        for (SubscribeEntity subscribeEntity : subscribeEntityList) {
            //获取预约时间
            String orderTime = subscribeEntity.getOrderTime();
            //截取
            String[] sDate = orderTime.split(" ");
            if (nowDate.equals(sDate[0])){
                //如果是同一天就进入
                subscribeEntities.add(subscribeEntity);

            }
        }

        //把处理后的集合传给page
        page.setList(subscribeEntities);
        System.out.println("·····················································");
        System.out.println(subscribeEntities);
        System.out.println("·····················································");
        page.setTotalCount(subscribeEntities.size());

        //获取参数
        String dishName = (String)params.get("dishName");
        if (!StringUtils.isNullOrEmpty(dishName) && !" ".equals(dishName)){
            List subscribeEntities1 = subscribeService.list(
                    new QueryWrapper<SubscribeEntity>().eq("dish_name", dishName));
            page.setList(subscribeEntities1);
            page.setTotalCount(subscribeEntities1.size());
        }





        return R.ok().put("page", page);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("menu:subscribe:info")
    public R info(@PathVariable("id") Integer id){
		SubscribeEntity subscribe = subscribeService.getById(id);

        return R.ok().put("order", subscribe);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("menu:subscribe:save")
    public R save(@RequestBody SubscribeEntity subscribe){

        System.out.println("================================================");
        System.out.println(subscribe);
        System.out.println("================================================");
        //自动生成所需字段
        subscribe.setSubCode(String.valueOf(System.currentTimeMillis()));
        subscribe.setOrderTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
        //获取点菜的菜名
        String dishName = subscribe.getDishName();
        //到菜谱查询是否存在
        MenuEntity menu = menuService.getOne(new QueryWrapper<MenuEntity>().eq("dish_name", dishName));
        if (null != menu){
            //获取物料
            String reqMate = menu.getReqMate();
            //查看所需物料
            if (reqMate.contains(",") || reqMate.contains("_")) {
                //分割
                String[] rms = reqMate.split(",");
                //遍历物料
                for (String rm : rms) {
                    //获取每一种物料
                    String[] s = rm.split("_");
                    //获取物料表
                    MatterEntity matterEntity = matterService.getOne(new QueryWrapper<MatterEntity>().eq("mate_name", s[0]).ge("current_inventory", Integer.parseInt(s[1])));
                    //如果是
                    if (null == matterEntity) {
                        return R.error(s[0] + "物料不足");
                    }
                //如果库存有物料就减少物料 （应该下单时减少）
                matterEntity.setCurrentInventory(matterEntity.getCurrentInventory()-Integer.parseInt(s[1]));
                matterService.updateById(matterEntity);
                    subscribeService.save(subscribe);

                    return R.ok();
                }
            }
        }


        return R.error("没有此菜品");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("menu:subscribe:update")
    public R update(@RequestBody SubscribeEntity subscribe){
		subscribeService.updateById(subscribe);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("menu:subscribe:delete")
    public R delete(@RequestBody Integer[] ids){
		subscribeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
